import { ref,  onMounted,h, getCurrentInstance} from "vue";
import { layer } from '@layui/layui-vue';
   
 
class MyData {
    public static   keyAdd(oldv:Array<String> , newv:Array<String>):Array<String>{
        let r=newv.filter(a=>!oldv.includes(a));
        return r;
    }
 
    public static   keyRemove(oldv:Array<String> , newv:Array<String>):Array<String>{
        let r=oldv.filter(a=>!newv.includes(a));
        return r;
    }
    public static getKeys(data:any):Array<String>{
        let r=data.map((a:any)=>a.id);
        return r;
    }
    public static getById(data:any,id:any):any{         
        if(!data){
            return null;
        }
        for(var i=0;i<data.length;i++){             
            if(data[i].Id==id){
                return data[i];
            }
            if(data[i].children){
                let cdata=this.getById(data[i].children,id);
                if(cdata){
                    return cdata;
                }
            }
        }
        return null;
    }


    public static addData(oldv:any,newv:any):void{
        let m=MyData.getById(oldv,newv.id);
        if(m==null)
        oldv.push(newv);
    }
    public static removeData(oldv:any,newv:any):void{
        MyData.removeDataById(oldv,newv.id);
    }
    public static removeDataById(oldv:any,id:any):any{
        for(let i=oldv.length-1;i>=0;i--){
            if(oldv[i].Id==id){ 
                return oldv.splice(i,1);
            }
        }
        return [];
    }
    public static addRowno(oldv :any,page:number,pageSize:number){
        if(!oldv){
            return;
        }
        let rowno:number=pageSize*( page-1)+1;
        oldv.forEach( (element: { rowno: number; })  => {
            element.rowno=rowno++; 
      });
    }
    public static toLayOptions(data:any):void{
        data.forEach( (o:any,i:number)=>{
            o.label=o.Name;
            o.value=o.Id;
             
        })
    }
    public static toLayTree(data:any):void{
        if(!data){
            return;
        }
        data.forEach( (o:any,i:number)=>{
            o.title=o.Name;
            o.id=o.Id;
            o.value=o.Id;
            o.label=o.Name;
            o.children=o.Children;
            if(o.children!=null&&o.children.length==0){
                o.children=null;
            }else{
                MyData.toLayTree(o.children);
            } 
        })
    }

    public static getTreeIds(data:any):Array<string>{
        let r=new Array<string>();
        if(!data){
            return r;
        }
        data.forEach( (o:any,i:number)=>{
            r.push(o.Id); 
            let r2=MyData.getTreeIds(o.children);
            r=r.concat(r2);
        })
        return r;
    }
    public static getPropV(obj:any,key:any):any{
        return obj[key];
    }
    public static join(rows:any,key:any):string{
        let r="";
        rows.forEach((element: any) => {
            r+=MyData.getPropV(element,key)+",";
        });
        return r.slice(0,r.length-1 ) 
    }


     
}
export default MyData